#import "BWGrid.h"
#import "BWJunkyard.h"


@implementation BWGrid

- (id) init
{
    if (self = [super init]) {

        [self setFabricWidth: 12];
        [self setFabricSpacing: 27];

        // the background is a very light gray, just dark enough
        // so that white stitches stand out a little bit
        NSColor *color;
        color = [NSColor colorWithCalibratedRed: 0.95
                         green: 0.95
                         blue: 0.95
                         alpha: 1.0];
        
        [self setBackgroundColor: color];

        [self setEdgeColor: [NSColor blackColor]];

        // this is the color that is used for the fabric grid.
        // the default lightGrayColor is just a skootch too 
        // dark
        color = [NSColor colorWithCalibratedRed: 0.78
                         green: 0.78
                         blue: 0.78
                         alpha: 1.0];
        [self setThreadColor: color];

        [self setEdgeWidth: 1.0];

        [self setTransientFabricWidth: BWGrid_NO_TRANSIENT_VALUE];
        [self setTransientFabricSpacing: BWGrid_NO_TRANSIENT_VALUE];
    }

    return (self);

} // init


- (void) dealloc
{
    [backgroundColor release];
    [transientBackgroundColor release];

    [threadColor release];
    [transientThreadColor release];

    [edgeColor release];
    [transientEdgeColor release];

    [super dealloc];

} // dealloc



- (void) drawInRect: (NSRect) rect
{
} // drawInRect


- (NSRect) rectForSquare: (BWGrid_Square) square
                    part: (BWGrid_SquarePart) part
{
    return (NSZeroRect);
} // squareRect


- (void) mapPoint: (NSPoint) point
         toSquare: (BWGrid_Square *) square
             part: (BWGrid_SquarePart *) part
  forceCenterOnly: (BOOL) forceCenterOnly
{
    *part = BWGrid_InvalidSquare;

    square->x = -99999;
    square->y = -99999;

} // mapPointToSquare



- (void) setFabricWidth: (float) newWidth
{
    fabricWidth = newWidth;
} // setFabricWidth


- (float) fabricWidth
{
    if (isNaN(transientFabricWidth)) {
        return (fabricWidth);
    } else {
        return (transientFabricWidth);
    }

} // fabricWidth


- (void) setTransientFabricWidth: (float) newWidth
{
    transientFabricWidth = newWidth;
} // setTransientFabricWidth


- (float) transientFabricWidth
{
    return (transientFabricWidth);
} // transientFabricWidth


- (void) setFabricSpacing: (float) newSpacing
{
    fabricSpacing = newSpacing;
} // setFabircSpacing


- (float) fabricSpacing
{
    if (isNaN(transientFabricSpacing)) {
        return (fabricSpacing);
    } else {
        return (transientFabricSpacing);
    }
} // fabircSpacing


- (void) setTransientFabricSpacing: (float) newSpacing
{
    transientFabricSpacing = newSpacing;
} // setTransientFabricSpacing


- (float) transientFabricSpacing
{
    return (transientFabricSpacing);
} // transientFabricWidth


- (void) setBackgroundColor: (NSColor *) newBackgroundColor
{
    [backgroundColor release];
    backgroundColor = [newBackgroundColor copy];

} // setBackgroundColor


- (NSColor *) backgroundColor
{
    if (transientBackgroundColor == NULL) {
        return (backgroundColor);
    } else {
        return (transientBackgroundColor);
    }

} // backgroundColor


- (void) setTransientBackgroundColor: (NSColor *) newBackgroundColor
{
    [transientBackgroundColor release];
    transientBackgroundColor = [newBackgroundColor copy];

} // setTransientBackgroundColor


- (NSColor *) transientBackgroundColor
{
    return (transientBackgroundColor);
} // transientBackgroundColor


- (void) setThreadColor: (NSColor *) newColor
{
    [newColor retain];
    [threadColor release];
    threadColor = newColor;

    // get red / gree / blue / alpha for the fancy grid drawing
    
    NSColor *rgbColor;

    // convert to RGB in case it's not in rgb space already
    rgbColor = [newColor colorUsingColorSpaceName: 
                             NSCalibratedRGBColorSpace];

    [rgbColor getRed: &threadRed
              green: &threadGreen
              blue: &threadBlue
              alpha: &threadAlpha];

} // setThreadColor


- (NSColor *) threadColor
{
    if (transientThreadColor == NULL) {
        return (threadColor);
    } else {
        return (transientThreadColor);
    }

} // threadColor



- (void) setTransientThreadColor: (NSColor *) newColor
{
    [newColor retain];
    [transientThreadColor release];
    transientThreadColor = newColor;

    if (newColor != NULL) {
        
        // get red / gree / blue / alpha for the fancy grid drawing
        
        NSColor *rgbColor;
        
        // convert to RGB in case it's not in rgb space already
        rgbColor = [newColor colorUsingColorSpaceName: 
                                 NSCalibratedRGBColorSpace];
        
        [rgbColor getRed: &threadRed
                  green: &threadGreen
                  blue: &threadBlue
                  alpha: &threadAlpha];
    } else {
        // rebuild the red/green/blue values
        [self setThreadColor: threadColor];
    }

} // setTransientThreadColor



- (NSColor *) transientThreadColor
{
    return (transientThreadColor);
} // transientThreadColor



- (void) setEdgeColor: (NSColor *) newColor
{
    [newColor retain];
    [edgeColor release];
    edgeColor = newColor;

} // setEdgeColor


- (NSColor *) edgeColor
{
    if (transientEdgeColor == NULL) {
        return (edgeColor);
    } else {
        return (transientEdgeColor);
    }

} // edgeColor


- (void) setTransientEdgeColor: (NSColor *) newColor
{
    [transientEdgeColor release];
    transientEdgeColor = [newColor copy];

} // setTransientEdgeColor


- (NSColor *) transientEdgeColor
{
    return (transientEdgeColor);

} // transientEdgeColor


- (void) setEdgeWidth: (float) newWidth
{
    edgeWidth = newWidth;
} // setEdgewidth


- (float) edgeWidth
{
    return (edgeWidth);
} // edgeWidth


- (void) setFlatGrid: (BOOL) onOff
{
    flatGrid = onOff;
} // setFlatGrid


- (BOOL) flatGrid
{
    return (flatGrid);
} // flatGrid



- (NSString *) description
{
    NSString *description;

    description = [NSString stringWithFormat:
                                @"fabricWidth: %f  fabricSpacing: %f  edgeWidth: %f  threadColor: %@   edgeColor: %@  flatGrid: %d",
                            fabricWidth, fabricSpacing, edgeWidth,
                            threadColor, edgeColor, flatGrid];

    return (description);

} // description


@end // BWGrid

